package com.qujia.station.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qujia.common.annotation.Log;
import com.qujia.common.constant.HttpStatus;
import com.qujia.common.core.controller.BaseController;
import com.qujia.common.core.domain.AjaxResult;
import com.qujia.common.core.domain.entity.SysUser;
import com.qujia.common.core.domain.model.LoginUser;
import com.qujia.common.core.page.TableDataInfo;
import com.qujia.common.enums.BusinessType;
import com.qujia.common.exception.CustomException;
import com.qujia.common.utils.SecurityUtils;
import com.qujia.common.utils.StringUtils;
import com.qujia.station.domain.*;
import com.qujia.station.domain.dto.*;
import com.qujia.station.domain.vo.EnterpriseAndStationVo;
import com.qujia.station.service.IEnterpriseService;
import com.qujia.station.service.IPayWayService;
import com.qujia.station.service.IShiftMaskService;
import com.qujia.station.service.IStationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 支付方式Controller
 *
 * @author qujia
 * @date 2020-12-08
 */
@RestController
@RequestMapping("/payWay/info")
@Api(tags = "支付渠道", description = "支付渠道")
public class PayWayController extends BaseController
{
    @Autowired
    private IPayWayService payWayService;

    @Autowired
    private IShiftMaskService shiftMaskService;

    @Autowired
    private IEnterpriseService enterpriseService;

    @Autowired
    private IStationService stationService;

    private static final Map<String,String> PayTypeMap = new HashMap<>();

    static {
        PayTypeMap.put("1","随行付");
        PayTypeMap.put("2","云闪付");
        PayTypeMap.put("3","通联");
        PayTypeMap.put("4","金控");
    }
    @GetMapping("/setShowPayWayIds")
    @ApiOperation(value = "要展示的商户号渠道列表传payWayId 用英文,隔开", notes = "要展示的商户号渠道列表传payWayId 用英文,隔开")
    public AjaxResult setShowPayWayIds(Station station){

        return AjaxResult.success( payWayService.setShowPayWayIds(station));
    }

    /**
     * 查询油站信息列表
     */
    @GetMapping("/list")
    @ApiOperation("查询油站支付渠道信息列表")
    public TableDataInfo list(PayWay payWay)
    {
        startPage();
        List<PayWay> tankList=payWayService.selectPayWayList(payWay);
        for(PayWay payWay1:tankList){
            ShiftClass shiftClass=new ShiftClass();
            shiftClass.setPayWayId(payWay1.getPayWayId());
            shiftClass.setDelFlag("0");
            payWay1.setShiftClasseList(shiftMaskService.selectShiftClassList(shiftClass));
            if(payWay1.getRemark()==null){
                payWay1.setPayTypeChinese(PayTypeMap.get(payWay1.getPayType())+"-");
            }else {
                payWay1.setPayTypeChinese(PayTypeMap.get(payWay1.getPayType())+"-"+payWay1.getRemark());
            }

        }
        return getDataTable(tankList);
    }

    /**
     * 切换支付渠道列表
     */
    @GetMapping("/autoPayWayList")
    @ApiOperation("切换支付渠道列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "beginTime", value = "开始时间", dataType = "String", paramType = "query", example = "2023-05-25 10:00:00"),
            @ApiImplicitParam(name = "endTime", value = "截止时间", dataType = "String", paramType = "query", example = "2023-05-25 18:00:00"),
            @ApiImplicitParam(name = "enterpriseId", value = "企业ID", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "stationId", value = "油站ID", dataType = "Long", paramType = "query")
    })
    public TableDataInfo autoPayWayList(AutoPayWayDto autoPayWayDto) {
        startPage();
        SysUser loginSysUser = SecurityUtils.getLoginSysUser();
        if (SecurityUtils.isPartner()) {
            if (StringUtils.isNotNull(autoPayWayDto.getEnterpriseId()) ||
                    StringUtils.isNotNull(autoPayWayDto.getStationNames()) ) {
                EnterpriseAndStationVo itemIds = stationService.getItemIds(loginSysUser);
                autoPayWayDto.setEnterpriseIds(itemIds.getEnterpriseIds());
            }
        }
        List<AutoPayWayDto> list = payWayService.autoPayWayList(autoPayWayDto);
        return getDataTable(list);
    }

    @ApiOperation(value = "新增切换支付渠道")
    @PostMapping("/setAutoPayWay")
    @ResponseBody
    public AjaxResult setAutoPayWay( @RequestBody AutoPayWayDto autoPayWayDto) throws Exception {

        int i = 0;
        return   payWayService.insertAutoPayWay(autoPayWayDto);

    }

    @ApiOperation(value = "获取油企下的商户号列表")
    @PostMapping("/getMnoByEnterpriseId")
    @ResponseBody
    public List<PayWay> getMnoByEnterpriseId(@RequestBody  Long id) throws Exception {

        List<PayWay> i = new ArrayList<>();
        i =   payWayService.getMnoByEnterpriseId(id);

        return i;
    }

    @ApiOperation(value = "设置商户的所有费率")
    @PostMapping("/setMnoAllRate")
    @ResponseBody
    public AjaxResult setMnoAllRate(@RequestBody RateSettingDto rateSettingDto) throws Exception {


        return payWayService.setMnoAllRate(rateSettingDto);
    }

    @ApiOperation(value = "设置商户的所有费率")
    @PostMapping("/selectMnoAllRate")
    @ResponseBody
    public RateSettingDto selectMnoAllRate(@RequestBody String mno) throws Exception {

        return payWayService.selectMnoAllRate(mno);
    }
    @ApiOperation(value = "test")
    @PostMapping("/selecttest")
    @ResponseBody
    public void selecttest() throws Exception {

         payWayService.selectMnoAllRateList();
    }


    @ApiOperation(value = "获取油企下的商户号列表")
    @PostMapping("/getSXFMnoServiceCharge")
    @ResponseBody
    public AjaxResult getSXFMnoServiceCharge(@RequestBody String id) throws Exception {

        List<PayWay> i = new ArrayList<>();
        return   payWayService.getSXFMnoServiceCharge(id);

    }



    @ApiOperation(value = "删除切换支付渠道")
    @PostMapping("/deleteAutoPayWay")
    @ResponseBody
    public AjaxResult deleteAutoPayWay(  Long id) throws Exception {

        int i = 0;
        i =   payWayService.deleteAutoPayWay(id);
        if (i == 1){
            return AjaxResult.success();
        }

        return AjaxResult.error("删除支付渠道设置失败");
    }

    @ApiOperation(value = "获取支付渠道详情")
    @PostMapping("/getAutoPayWayInfo")
    @ResponseBody
    public List<AutoPayWayInfo> getAutoPayWayInfo(  Long id) throws Exception {

        List<AutoPayWayInfo> i = new ArrayList<>();
        i =   payWayService.getAutoPayWayInfo(id);

        return i;
    }

    @ApiOperation(value = "selectAutoPayWay")
    @GetMapping(path = {"/selectAutoPayWay","/anony/selectAutoPayWay"})
    @ResponseBody
    public void selectAutoPayWay(int wayPayWay,Long stationId,String oilGun) throws Exception {


       payWayService.selectAutoPayWay(1,stationId,oilGun);


    }



    @ApiOperation(value = "编辑切换支付渠道")
    @PostMapping("/editAutoPayWay")
    @ResponseBody
    public AjaxResult editAutoPayWay( @RequestBody AutoPayWayDto autoPayWayDto) throws Exception {

        int i = 0;
        i =   payWayService.updateAutoPayWay(autoPayWayDto);
        if (i == 1){
            return AjaxResult.success();
        }

        return AjaxResult.error("编辑支付渠道设置失败");
    }

    /**
     * 新增支付方式
     */
    @Log(title = "新增支付方式", businessType = BusinessType.INSERT)
    @PostMapping
    @Transactional
    @ApiOperation("新增支付方式")
    public AjaxResult add(@RequestBody PayWay payWay)
    {
        LoginUser current=SecurityUtils.getLoginUser();
        int flage=0;
       if(payWay.getEnterpriseId()==null){
           return toAjax(0);
       }

               payWayService.insertPayWay(payWay);
        int payId= Math.toIntExact(payWay.getPayWayId());
        flage=payId;
        if(payWay.getShiftClasseList()!=null&&payWay.getShiftClasseList().size()>0){
            for(ShiftClass shiftClass:payWay.getShiftClasseList()) {
                shiftClass.setPayWayId(Long.parseLong(payId+""));
                shiftClass.setCreateTime(new Date());
                shiftClass.setCreateUser(current.getUser().getUserId());
                flage=shiftMaskService.insertShiftClass(shiftClass);
            }

        }
        // 更新油企修改时间
        try {
            Enterprise enterprise = new Enterprise();
            enterprise.setEnterpriseId(payWay.getEnterpriseId());
            enterpriseService.updateEnterprise(enterprise);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return toAjax(flage);
    }

    /**
     * 获取油站信息详细信息
     */
    @GetMapping(value = "/{payWayId}")
    public AjaxResult getInfo(@PathVariable("payWayId") Long payWayId)
    {
        PayWay payWay=payWayService.selectPayWayById(payWayId);

        return AjaxResult.success(payWay);
    }

    /**
     * 修改油站信息
     */
    @Log(title = "修改支付方式", businessType = BusinessType.UPDATE)
    @PutMapping
    @Transactional
    public AjaxResult edit(@RequestBody PayWay payWay)
    {
        int flag=0;
        LoginUser current= SecurityUtils.getLoginUser();

        if(payWay.getShiftClasseList()!=null&&payWay.getShiftClasseList().size()>0){
            ShiftClass shiftClass1=new ShiftClass();
            shiftClass1.setPayWayId(payWay.getPayWayId());
            shiftClass1.setDelFlag("0");
            List<ShiftClass> shiftClassList=shiftMaskService.selectShiftClassList(shiftClass1);
            if(shiftClassList!=null&&shiftClassList.size()>0){
                for(ShiftClass shiftClass2:shiftClassList){
                    shiftClass2.setDelFlag("1");
                    shiftClass2.setUpdateUser(current.getUser().getUserId());
                    shiftClass2.setUpdateTime(new Date());
                    flag = shiftMaskService.updateShiftClass(shiftClass2);
                }
            }
            for(ShiftClass shiftClass:payWay.getShiftClasseList()) {
                shiftClass.setPayWayId(payWay.getPayWayId());
                shiftClass.setCreateTime(new Date());
                shiftClass.setCreateUser(current.getUser().getUserId());
                shiftClass.setShiftClassId(null);
                flag=shiftMaskService.insertShiftClass(shiftClass);
            }

        }
        // 更新油企修改时间
        try {
            Enterprise enterprise = new Enterprise();
            enterprise.setEnterpriseId(payWay.getEnterpriseId());
            enterpriseService.updateEnterprise(enterprise);
        } catch (Exception e) {
            e.printStackTrace();
        }
        flag=payWayService.updatePayWay(payWay);
        return toAjax(flag);
    }



}
